package com.effectiv.gooruda.rest.api.controller;

import java.util.List;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.effectiv.gooruda.rest.api.domain.Role;
import com.effectiv.gooruda.rest.api.operation.RoleOperations;


@Controller
@RequestMapping("/api/role")
public class RoleContoller implements RoleOperations {

	@Autowired
	@Qualifier("roleServiceImpl")
	private RoleOperations operations;

	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody
	Role save(@Valid @RequestBody Role role) {
		return operations.save(role);
	}

	@RequestMapping(value = "/findByName/{roleName}", method = RequestMethod.GET)
	public @ResponseBody
	Role findByName(@PathVariable("roleName") String roleName) {
		return operations.findByName(roleName);
	}

	@RequestMapping(value = "/addpermission/{roleName}/{permissionName}", method = RequestMethod.POST)
	public @ResponseBody
	Role addPermission(@PathVariable("roleName") String role,
			@PathVariable("permissionName") String permission) {
		return operations.addPermission(role, permission);
	}

	@RequestMapping(value = "/addpermissions/{roleName}", method = RequestMethod.POST)
	public @ResponseBody
	Role addPermissions(@PathVariable("roleName") String role,
			@RequestBody List<String> permissions) {
		return operations.addPermissions(role, permissions);
	}


	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public @ResponseBody
	Role findOne(@PathVariable("id") String id) {
		return operations.findOne(id);
	}

}
